/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cadastro.java
 *
 * Created on 18/12/2011, 10:39:24
 */
package Vista;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import modelo.Aluno;
import Controle.AlunoControle;
import excecoes.CpfJaExistenteException;

/**
 *
 * @author Claudia
 */
public class Cadastro extends javax.swing.JPanel {

	AlunoControle alunoControle = AlunoControle.getInstance();
	
	/** Creates new form Cadastro */
    public Cadastro() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        txtCPF = new javax.swing.JTextField();
        txtNascimento = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        lblCPF = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        lblNascimento = new javax.swing.JLabel();
        btCriarAluno = new javax.swing.JButton();
        pswSenha = new javax.swing.JPasswordField();
        pswConfirmar = new javax.swing.JPasswordField();
        lblConfirmar = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        lblUsuario = new javax.swing.JLabel();
        btVoltar = new javax.swing.JButton();
        lblNomeErro = new javax.swing.JLabel();
        lblCpfErro = new javax.swing.JLabel();
        lblNascimentoErro = new javax.swing.JLabel();
        lblUsuarioErro = new javax.swing.JLabel();
        lblSenhaErro = new javax.swing.JLabel();
        lblConfirmarErro = new javax.swing.JLabel();
        lblMensagem = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Cadastro");

        txtNome.setText("aa");
        txtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeActionPerformed(evt);
            }
        });

        txtCPF.setText("123");
        txtCPF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCPFActionPerformed(evt);
            }
        });

        txtNascimento.setText("10/03/1986");
        txtNascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNascimentoActionPerformed(evt);
            }
        });

        lblNome.setText("Nome:");

        lblCPF.setText("CPF:");

        lblSenha.setText("Senha:");

        lblNascimento.setText("Nascimento:");

        btCriarAluno.setText("Criar");
        btCriarAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCriarAlunoActionPerformed(evt);
            }
        });

        pswSenha.setText("aa");
        pswSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pswSenhaActionPerformed(evt);
            }
        });

        pswConfirmar.setText("aa");

        lblConfirmar.setText("Confirmar:");

        txtUsuario.setText("aa");

        lblUsuario.setText("Usuario:");

        btVoltar.setText("Voltar");
        btVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVoltarActionPerformed(evt);
            }
        });

        lblNomeErro.setText("  ");

        lblCpfErro.setText("  ");

        lblNascimentoErro.setText("  ");

        lblUsuarioErro.setText("  ");

        lblSenhaErro.setText(" ");

        lblConfirmarErro.setText(" ");

        lblMensagem.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblCPF)
                            .addComponent(lblSenha)
                            .addComponent(lblConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsuario)
                            .addComponent(lblNascimento)
                            .addComponent(lblNome))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pswConfirmar, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(pswSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(txtCPF, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(txtNascimento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btVoltar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btCriarAluno))
                            .addComponent(lblMensagem))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblConfirmarErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblSenhaErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblUsuarioErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblNascimentoErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblCpfErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblNomeErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(44, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNomeErro)
                    .addComponent(lblNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCpfErro)
                    .addComponent(lblCPF))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNascimentoErro)
                    .addComponent(lblNascimento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblUsuarioErro)
                    .addComponent(lblUsuario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pswSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSenhaErro, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pswConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblConfirmarErro, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblConfirmar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCriarAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(lblMensagem)
                .addContainerGap(21, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void txtCPFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCPFActionPerformed
// TODO add your handling code here:
//	this.aluno.setCpf(txtCPF.getText());
	
}//GEN-LAST:event_txtCPFActionPerformed

private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeActionPerformed
// TODO add your handling code here:
//	this.aluno.setNome(txtNome.getText());
}//GEN-LAST:event_txtNomeActionPerformed

private void txtNascimentoActionPerformed(java.awt.event.ActionEvent evt){
	
}

private void btCriarAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCriarAlunoActionPerformed
	String path = "images/erro2.png";
	boolean erro = false;
	try {
		BufferedImage myPicture = ImageIO.read(new File(path));
		
		String nome = txtNome.getText() + "";
		System.out.println("Nome= " + nome);
		if (nome.equals("")) {
			lblNomeErro.setIcon(new ImageIcon(myPicture));
			lblNomeErro.setVisible(true);
			erro = true;
		} else {
			lblNomeErro.setIcon(null);
		}
		String cpf = txtCPF.getText() + "";
		if (cpf.equals("")) {
			lblCpfErro.setIcon(new ImageIcon(myPicture));
			erro = true;
		} else {
			lblCpfErro.setIcon(null);
		}
		String nascimento = txtNascimento.getText();
		Date date = null;
		if(nascimento.equals("")) {
			lblNascimentoErro.setIcon(new ImageIcon(myPicture));
			erro = true;
		} else {
			lblNascimentoErro.setIcon(null);
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			date = format.parse(nascimento);		
		}
		
		String login = txtUsuario.getText();
		if (login.equals("")){
			lblUsuarioErro.setIcon(new ImageIcon(myPicture));
			erro = true;
		} else {
			lblUsuarioErro.setIcon(null);
		}
		
		String senha = new String(pswSenha.getPassword());
		String confirmar = new String(pswConfirmar.getPassword());
		if (!senha.equals(confirmar)){
			lblSenhaErro.setIcon(new ImageIcon(myPicture));
			lblConfirmarErro.setIcon(new ImageIcon(myPicture));
		} else {
			lblSenhaErro.setIcon(null);
			lblConfirmarErro.setIcon(null);
		}
		if(erro) {
			return;
		}
	
		alunoControle.criarAluno(nome, cpf, date, senha, login);
		lblMensagem.setText("Aluno criado com sucesso!");
	} catch (CpfJaExistenteException e) {
		e.printStackTrace();
		lblMensagem.setText("Cpf ja cadastrado no sistema!");
	}catch (ParseException e) {
		lblNascimentoErro = new JLabel(new ImageIcon(path));
		erro = true;
	} catch (IOException e1) {
		e1.printStackTrace();
	}
	Login lo = new Login();
	JPanel p3 = new JPanel();
	JFrame frame3 = new JFrame("Pagina Inicial");
	p3.add(lo);
	p3.setVisible(true);
	frame3.getContentPane().add(p3);
	frame3.pack();
	frame3.show();
	frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}//GEN-LAST:event_btCriarAlunoActionPerformed

private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVoltarActionPerformed
    
}//GEN-LAST:event_btVoltarActionPerformed

private void pswSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pswSenhaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_pswSenhaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCriarAluno;
    private javax.swing.JButton btVoltar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblCPF;
    private javax.swing.JLabel lblConfirmar;
    private javax.swing.JLabel lblConfirmarErro;
    private javax.swing.JLabel lblCpfErro;
    private javax.swing.JLabel lblMensagem;
    private javax.swing.JLabel lblNascimento;
    private javax.swing.JLabel lblNascimentoErro;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblNomeErro;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JLabel lblSenhaErro;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JLabel lblUsuarioErro;
    private javax.swing.JPasswordField pswConfirmar;
    private javax.swing.JPasswordField pswSenha;
    private javax.swing.JTextField txtCPF;
    private javax.swing.JTextField txtNascimento;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
